<?php /*
Copyright 2005 Dan Boitnott (dan@lclinux.org)

This file is part of phpTasks.

phpTasks is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

phpTasks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with phpTasks; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/ ?>
<?php require("common.php"); ?>
<?php

if (!is_numeric($HTTP_VARS["taskid"])) {
  echo ("Invalid taskid! - 1");
  exit;
} else {
  $taskid = $HTTP_VARS["taskid"];
}

if ($r = fetchrow($conn, "tasks", "taskid=" . $taskid)) {
  if ((!$USER["viewalltasks"]) && ($r["assigned_to"] <> $USER["userid"]) && ($r["assigned_by"] <> $USER["userid"])) {
    echo ("You are not authorized to view other's tasks!");
    exit;
  }
} else {
  echo ("Invalid taskid!");
  exit;
}

$bn = $HTTP_VARS['f'];

if (strstr($bn, '/')) {
  echo "Invalid file name.";
  exit;
}

$fn = $CONFIG['file_store'] . "/$taskid/$bn";
$ext = strtolower(substr(strrchr($filename, "."), 1));

if (!is_file($fn)) {
  echo "File not found.";
  exit;
}

if (!is_readable($fn)) {
  echo "File not readable.";
  exit;
}

$mimes = mimeTypes($CONFIG['mime_file']);

if (isset($mimes[$ext])) {
  $mtype = $mimes[$ext];
} else {
  $mtype = "application/unknown";
}

header("Content-Type: $mtype");
header('Content-Disposition: attachment; filename="' . $bn . '"');

if (is_numeric($HTTP_VARS['r'])) {
  $r = $HTTP_VARS['r'];
  passthru("co -p$r -q $fn");
} else {
  header("Content-Length: " . @filesize($fn));
  readfile($fn); 
}
exit;
?>
